package com.gking.centerSystem.service.RoleRoutes;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gking.centerSystem.common.ProgramException;
import com.gking.centerSystem.common.Result;
import com.gking.centerSystem.entity.RoleRoutes;
import com.gking.centerSystem.entity.Routes;
import com.gking.centerSystem.entity.enums.UserDataSetTypeEnum;
import com.gking.centerSystem.mapper.RoleRoutesMapper;
import com.gking.centerSystem.service.Routes.RoutesService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Slf4j
@Service
public class RoleRoutesServiceImpl extends ServiceImpl<RoleRoutesMapper, RoleRoutes> implements RoleRoutesService {

    @Autowired
    private RoutesService routesService;

    /**
     * 获取角色路由详情
     *
     * @param roleId 角色id
     * @return s
     */
    @Override
    public Result<List<Routes>> getRoleRoutesDetail(String roleId) {
        try {
            if (roleId.isEmpty()) return Result.error("请将参数补充完整");

            LambdaQueryWrapper<RoleRoutes> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(RoleRoutes::getRoleId, roleId);
            List<RoleRoutes> list = this.list(wrapper);
            if (list.isEmpty()) return Result.success("该角色路由为空");

            List<String> idList = new ArrayList<>();
            for (RoleRoutes roleRoutes : list) idList.add(roleRoutes.getRouteId());

            LambdaQueryWrapper<Routes> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.in(Routes::getId, idList);
            queryWrapper.eq(Routes::getIsDelete, 0);
            List<Routes> routesList = routesService.list(queryWrapper);

            return Result.success("获取角色路由列表成功！", routesList);
        } catch (Exception e) {
            log.error("获取角色路由详情失败，错误信息：{}", e.getMessage());
            throw new ProgramException();
        }
    }

    /**
     * 为角色批量设置路由
     *
     * @param roleId       角色id
     * @param type         类型 （refresh, add, remove）
     * @param routesIdList 权限id 列表
     * @return s
     */
    @Transactional
    @Override
    public Result<String> setRoleRoutes(String roleId, String type, List<String> routesIdList) {

        if (roleId.isEmpty() || routesIdList.isEmpty() || type.isEmpty())
            return Result.error("请将参数补充完整");
        if (UserDataSetTypeEnum.getEnumByValue(type) == null) return Result.error("type参数错误");

        try {
            // 删除角色路由
            if (Objects.equals(type, UserDataSetTypeEnum.REMOVE.getValue())) {
                LambdaQueryWrapper<RoleRoutes> wrapper = new LambdaQueryWrapper<>();
                wrapper.eq(RoleRoutes::getRoleId, roleId);
                wrapper.in(RoleRoutes::getRouteId, routesIdList);

                this.remove(wrapper);
                return Result.success("删除角色路由成功");
            }

            List<RoleRoutes> roleRoutesList = new ArrayList<>();
            for (String routesId : routesIdList) {
                RoleRoutes roleRoutes = new RoleRoutes();
                roleRoutes.setRoleId(roleId);
                roleRoutes.setRouteId(routesId);
                roleRoutesList.add(roleRoutes);
            }

            //重新为角色设置路由，全部重新设置
            if (Objects.equals(type, UserDataSetTypeEnum.REFRESH.getValue())) {
                LambdaQueryWrapper<RoleRoutes> wrapper = new LambdaQueryWrapper<>();
                wrapper.eq(RoleRoutes::getRoleId, roleId);

                this.remove(wrapper);
                this.saveBatch(roleRoutesList);
                return Result.success("设置角色路由成功");
            }

            //为角色在已有基础上添加路由
            if (Objects.equals(type, UserDataSetTypeEnum.ADD.getValue())) {
                LambdaQueryWrapper<RoleRoutes> wrapper = new LambdaQueryWrapper<>();
                wrapper.eq(RoleRoutes::getRoleId, roleId);
                wrapper.in(RoleRoutes::getRouteId, routesIdList);
                if (this.count(wrapper) > 0) return Result.error("角色路由已存在");

                this.saveBatch(roleRoutesList);
                return Result.success("设置角色路由成功");
            }

            return Result.error("设置角色路由失败");
        } catch (Exception e) {
            log.error("设置角色路由失败，错误信息：{}", e.getMessage());
            throw new ProgramException();
        }
    }
}
